import recommend from '@/components/waterfall/recommend.vue'
export default {
	components: {
		recommend
	},
	data() {
		return {
			pageLoading: false,
			statusBarHeight: 0,
			isHeight: 0,
			navHight: 0,
			loadStatus: ['loading', 'loading', 'loading', 'loading'],
			swiperCurrent: 0, //swiper当前
			carlist: {
				0: [],
				1: [],
				2: [],
				3: []
			},
			tabTop: false, // tab吸顶时触发的开关
			choise: {}, //传过来的面包屑
			//面包屑
			history: {
				carTypeId: '',
				buyTypeId: '',
				rangeValues: []
			},
			background: {
				'background': '#FBD220'
			},
			current: 0,
			list: [{
					name: '推荐'
				}, {
					name: '1成首付'
				}, {
					name: '以租代售',
				},
				{
					name: '巨能降',
				},
			],
			carIndex: [1, 1, 1, 1], //当前页
			carTypeId: 0,
			buyTypeId: 0,
			rangeValues: [0, 70],
			newMaxValue: 0,
			storeList: {}, //店铺数据
			range: []
		}
	},
	onLoad(data) {
		let list = JSON.parse(data.list)

		this.storeList = list
	},
	onShow() {
		uni.$on('type2', data => {

			this.history.carTypeId = data.carTypeName
			this.history.buyTypeId = data.onBuyTypeId
			this.history.rangeValues = data.rangeValues

			this.comunicationOff()
		})
		uni.$on('rangeValues1', data => {
			console.log(data)
			this.carTypeId = data.carTypeId //图
			this.buyTypeId = data.buyTypeId
			this.rangeValues = data.rangeValues
			this.range = data.rangeValues
			this.offValue()
		})
	},
	created() {
		//#ifdef APP-PLUS || MP-WEIXIN
		this.statusBarHeight = 0
		//#endif
		//#ifdef H5
		this.statusBarHeight = 44
		//#endif
		this.getHome()
	},
	computed: {
		stickyHeight() {
			// return Math.floor(uni.upx2px(this.statusBarHeight))
			return Math.floor(Number(this.statusBarHeight) / (uni.upx2px(Number(this.statusBarHeight)) / Number(this
				.statusBarHeight))) || 0
		}
	},
	// onReady() {
	//  //获取导航栏高度

	//  const query = uni.createSelectorQuery().in(this)
	//  query.select('.nav').boundingClientRect(data => {
	//    // this.height = data.height
	//    this.navHight = data.height
	//  }).exec()
	// },
	watch: {
		range(val) {
			this.carIndex[this.swiperCurrent] = 1
			this.carlist[this.swiperCurrent] = []
			if (val[1] === 70) {
				this.newMaxValue = 0

			} else {
				this.newMaxValue = val[1]
			}
			this.rangeValues[0] = val[0]
			console.log('range')
			this.getHome()
		},
	},
	methods: {
		more() {
			this.getHome()
		},
		onPullDownRefresh() {
			this.carlist[this.swiperCurrent] = []
			this.carIndex[this.swiperCurrent] = 1
			this.getHome()
		},
		moreStatus(status) {
			this.loadStatus[this.swiperCurrent] = status
		},

		async getHome() {
			this.loadStatus[this.swiperCurrent] = 'loading'
			uni.showLoading({
				title: '加载中',
				mask: true
			})
			try {
				let data = {
					'pageIndex': this.carIndex[this.swiperCurrent],
					'pageSize': 10,
					'brandId': this.storeList.brandId,
					'lowprice': this.rangeValues[0] * 10000,
					'highprice': this.newMaxValue * 10000,
					'cartype': this.buyTypeId === 0 ? this.swiperCurrent : this.buyTypeId,
					'carBodyModel': this.carTypeId
				}
				const res = await this.$api.buyCar.getBrandSupplyNextLevelList(data)
				this.carlist[this.swiperCurrent] = [...this.carlist[this.swiperCurrent], ...res.data.list]
				uni.stopPullDownRefresh()
				uni.hideLoading()
				this.$nextTick(() => {
					this.pageLoading = true
				})
				let length = res.data.list.length
				if (res.data.total <= 10) {
					this.loadStatus[this.swiperCurrent] = 'nomore'
				} else {
					this.moreStatus(length !== 0 ? 'loading' : 'nomore')
				}
				if (length === 0) return false
				this.carIndex[this.swiperCurrent] += 1 // 当前页加1
			} catch (e) {
				uni.stopPullDownRefresh()
				uni.hideLoading()
			}
		},
		// 删除购车类型
		delcarTypeId() {
			this.carlist[this.swiperCurrent] = []
			this.carIndex[this.swiperCurrent] = 1

			this.history.carTypeId = ''
			this.carTypeId = 0
			this.getHome()
		},
		delbuyTypeId() {
			this.carlist[this.swiperCurrent] = []
			this.carIndex[this.swiperCurrent] = 1
			this.buyTypeId = 0
			this.history.buyTypeId = ''

			this.getHome()
		},
		delrangeValues() {
			this.carlist[this.swiperCurrent] = []
			this.carIndex[this.swiperCurrent] = 1
			this.history.rangeValues = []
			this.newMaxValue = 0
			this.rangeValues = [0, 70]
			this.getHome()

		},
		//移除事件
		comunicationOff() {
			uni.$off('type')
		},
		offValue() {
			uni.$off('rangeValues3')
		},

		// 重置
		reset() {
			this.carlist[this.swiperCurrent] = []
			this.carIndex[this.swiperCurrent] = 1
			this.newMaxValue = 0
			this.rangeValues = [0, 70]
			this.carTypeId = 0
			this.history.rangeValues = []
			this.history.carTypeId = ''
			this.history.buyTypeId = ''
			// if(this.swiperCurrent!==0){
			//   this.swiperCurrent = 0
			// }else{
			this.getHome()
			// }

		},
		//筛选
		choice() {
			uni.navigateTo({
				url: '/pages_common/pages_index/screen/screen?type=110' + '&id=' + this.storeList.brandId
			})
		},
		swiperChange(i) {
			this.swiperCurrent = i.detail.current
			let e = i.detail.current
			if (this.carlist[e].length === 0) {
				this.$u.debounce(this.getHome, 500)
			}
		},
		// tabs通知swiper切换
		tabsChange(index) {
			this.swiperCurrent = index
		},
		// swiper-item左右移动，通知tabs的滑块跟随移动
		transition(e) {
			let dx = e.detail.dx
			this.$refs.uTabs.setDx(dx)
		},
		// 由于swiper的内部机制问题，快速切换swiper不会触发dx的连续变化，需要在结束时重置状态
		// swiper滑动结束，分别设置tabs和swiper的状态
		animationfinish(e) {
			let current = e.detail.current
			this.$refs.uTabs.setFinishCurrent(current)
			this.swiperCurrent = current
			this.current = current
		},
		// scroll-view到底部加载更多
		onreachBottom() {
			if (this.loadStatus[this.swiperCurrent] !== 'nomore') {
				this.getHome()
			}
		}
	}
}
